package com.suning.yace;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

public class Launcher {

    private static EventLoopGroup group = new NioEventLoopGroup();

    public static Properties config = null;

    private static String localIp;

    private static List<Connection> devices = new ArrayList<Connection>();

    public static void main(String[] args) {
        localIp = IPUtils.getLocalIP();
        int count = (args.length == 0) ? 10000 : Integer.valueOf(args[0]);
        makeAllClients(count);
        checkHeartbeat();
    }

    private static void makeAllClients(int count) {
        for (int i = 0; i < count; i++) {
            try {
                String custNum = localIp + "_" + i;
                Connection conn = new Connection(custNum, group);
                conn.start();
                devices.add(conn);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private static void checkHeartbeat() {
        while (true) {
            try {
                Thread.sleep(5000);
                System.out.println("*****************************************************");
                for (Connection device : devices) {
                    device.checkHeartbeat();
                }
            } catch (Exception ex) {
            	System.out.println("Exception " + ex);
            }
        }
    }


}
